﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Settings;
using Utils;
using System.Data;

namespace PanasonicWarranty.Business
{
    public partial class pa_Warranty : BaseClass
    {
        public static DateTime? GetWarrantyExtentRegisterDate(string modelCode, string productCode, DateTime registerDate)
        {
            try
            {
                return (DateTime)ExecuteScalar("usp_GetWarrantyExtentRegisterDate", CommandType.StoredProcedure, 
                    new SqlParameter("@ModelCode", modelCode), new SqlParameter("@ProductCode", productCode), 
                    new SqlParameter("@RegisterDate", registerDate));
            }
            catch
            {
                return null;
            }
        }

        public static bool CheckExists(string modelCode, string productCode)
        {
            return (bool)ExecuteScalar("usp_pa_Warranty_CheckExists", CommandType.StoredProcedure, 
                new SqlParameter("@ModelCode", modelCode), new SqlParameter("@ProductCode", productCode));
        }

        public static bool Insert(SqlTransaction trans, pa_WarrantyWaiting objWaiting, int warantySatusID, int UserID, string description)
        {
            try
            {
                pa_WarrantyPackage warrantypackage = pa_WarrantyPackage.GetOne(objWaiting.WarrantyPackageCode);
                SqlParameter[] param = new SqlParameter[]
                    {
                        new SqlParameter("@ModelCode", objWaiting.ModelCode),
                        new SqlParameter("@ProductCode", objWaiting.ProductCode),
                        new SqlParameter("@AgencyID", objWaiting.AgencyID),
                        new SqlParameter("@CustomerPhone",objWaiting.CustomerPhone),
                        new SqlParameter("@WarrantyPackageID", string.IsNullOrEmpty(objWaiting.WarrantyPackageCode)? 
                                                            (object)DBNull.Value: 
                                                            warrantypackage.WarrantyPackageID),
                        new SqlParameter("@CreatedDate", objWaiting.ModifiedDate ?? objWaiting.CreatedDate),
                        new SqlParameter("@RegisterDate",objWaiting.RegisterDate),
                        new SqlParameter("@ConfirmDate", DateTime.Now),
                        new SqlParameter("@ExpiredDate", objWaiting.WarrantyType == eWarrantyType.DK?
                                                        pa_Model.GetExpiredDate(objWaiting.ModelCode, objWaiting.RegisterDate):
                                                        pa_WarrantyPackage.GetExpriredDate(warrantypackage.WarrantyPackageID, objWaiting.RegisterDate)),
                        new SqlParameter("@UserID", UserID),
                        new SqlParameter("@WarrantyType", objWaiting.WarrantyType),
                        new SqlParameter("@Description", description),
                        new SqlParameter("@RegisteredFrom",objWaiting.RegisteredFrom),
                        new SqlParameter("@RegisteredPhone", objWaiting.RegisteredPhone ?? (object)DBNull.Value),
                        new SqlParameter("@WarrantyStatusID",warantySatusID)
                    };

                var retvalue = SqlHelper.ExecuteScalar(trans, CommandType.StoredProcedure, "usp_pa_Warranty_Insert", param);
                //Xóa dữ liệu bảng tạm
                SqlHelper.ExecuteNonQuery(trans, CommandType.StoredProcedure, "usp_pa_WarrantyWaiting_Delete", new SqlParameter("@WarrantyWattingID", objWaiting.WarrantyWattingID));
                if (retvalue != null)
                    return (bool)retvalue;
                return false;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
